import { defineStore } from 'pinia'
import { ref,reactive} from 'vue';
import {authorityManagement} from "@/apis/login"

export const useUserStore = defineStore('user',() => {
    const refreshToken=ref("");
    const token=ref("");
    const userInfo = reactive({
        id: "",
        email: "", 
        nickName:"",
        enabled:"", 
        roles:"", 
        sex: "", 
        username:"",
        createTime:"",
        updateTime:"",
        img: "",
    });
    const menuList=ref([]);
    const getUserInfo = (options) => {
        for (const key in userInfo) {
            options[key]&&(userInfo[key]=options[key])
        }
    };
    const getToken=(options)=>{
        token.value=options;
    }
    const getRefreshToken=(options)=>{
        refreshToken.value=options;
    }
    const clearUserInfo=()=>{
        for (const key in userInfo){
            userInfo[key]="";
        }
        token.value="";
        refreshToken.value="";
    };
    const logout=()=>{
        refreshToken.value="",
        token.value="";
        for (let key in userInfo) {
            userInfo[key]="";
        }
    }
    const changeMenuList=(options)=>{
        menuList.value=options;
    }
    
    return {
        userInfo,
        refreshToken,
        token,
        menuList,
        getUserInfo,
        clearUserInfo,
        getToken,
        getRefreshToken,
        logout,
        changeMenuList
    }
}, {
    persist: true,
})